package xyz.bali16.module.netdisk.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bali2 on 1/11/2021.
 * @version 1.0
 */
public class MatchUtil {
    /**
     * 文件尾部的suffix增一 - Exist(1) 执行后 - Exist(2)
     * @param s
     * @return
     */
    public static String suffixIndexAdd(String s, String suffix) {
        Integer index = 1;
        String regex = "\\(([0-9]*)\\)";
        final Pattern pat = Pattern.compile(suffix + regex);
        Matcher mat = pat.matcher(s);
        if (mat.find()) {
            index = Integer.valueOf(mat.group(1)) + 1;
            return s.replaceAll(suffix + regex, suffix + "(" + index + ")");
        }
        return s + " " + suffix + "(1)";
    }
    public static Integer suffixIndex(String s) {
        Integer index = 1;
        final Pattern pat = Pattern.compile("Exist\\(([0-9]*)\\)");
        Matcher mat = pat.matcher(s);
        if(mat.find()){
            index = Integer.valueOf(mat.group(1));
        }
        return index;
    }
}
